package cn.iocoder.yudao.module.mybusiness.service.impl;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mybusiness.domain.Facility;
import cn.iocoder.yudao.module.mybusiness.domain.ProductionRecord;
import cn.iocoder.yudao.module.mybusiness.mapper.FacilityMapper;
import cn.iocoder.yudao.module.mybusiness.mapper.RecordMapper;
import cn.iocoder.yudao.module.mybusiness.mapper.TaskMapper;
import cn.iocoder.yudao.module.mybusiness.service.AnalyzeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

@Service
public class AnalyzeServiceImpl implements AnalyzeService {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private RecordMapper recordMapper;

    @Autowired
    private FacilityMapper facilityMapper;

    @Override
    public Map getIndexBlock() {
        Map map = new HashMap();
        //任务数
        Long taskNum = taskMapper.selectCount();

        //生产总数
        Long recordNum = recordMapper.selectCount();

        String standardChance = null;
        //合格数
        Long standardNum = recordMapper.selectCount(new LambdaQueryWrapperX<ProductionRecord>().eq(ProductionRecord::getResult,1));

        if(recordNum !=null){
            double percentage = ((double)  standardNum/ recordNum) * 100;
            // 格式化输出百分比，保留两位小数
            DecimalFormat formatter = new DecimalFormat("#0");
            standardChance = formatter.format(percentage);
        }

        //设备总数
        Long facNum = facilityMapper.selectCount(new LambdaQueryWrapperX<Facility>().eq(Facility::getIsDelete, 0));

        //运行设备数
        Long operationFacNum = facilityMapper.selectCount(new LambdaQueryWrapperX<Facility>().eq(Facility::getIsDelete, 0).eq(Facility::getStatus, 2));
        map.put("taskNum",taskNum==null?0:taskNum);
        map.put("recordNum",recordNum==null?0:recordNum);
        map.put("standardNum",standardNum==null?0:standardNum);
        map.put("standardChance",standardChance==null?0:standardChance); //合格率
        map.put("facNum",facNum==null?0:facNum);
        map.put("operationFacNum",operationFacNum==null?0:operationFacNum);
        return map;
    }
}
